package com.`fun`.funlivewallpaper

import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.text.TextUtils
import com.airbnb.lottie.LottieDrawable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


object WallpaperHelper {
    private fun getLauncherWallpaper(context: Context): Bitmap {
        val wallpaperManager = WallpaperManager.getInstance(context)
        val drawable = wallpaperManager.drawable
        return drawableToBitmap(drawable)
    }

    private fun drawableToBitmap(drawable: Drawable?): Bitmap {
        val bitmap = Bitmap.createBitmap(
            drawable!!.intrinsicWidth,
            drawable.intrinsicHeight,
            if (drawable.opacity != -1) Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565
        )
        val canvas = Canvas(bitmap)
        drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
        drawable.draw(canvas)
        return bitmap
    }

    fun getDefaultWallpaperName(context: Context?): String {
        val mWallpaperManager = WallpaperManager.getInstance(context)
        return if (null != mWallpaperManager && null != mWallpaperManager.wallpaperInfo && !TextUtils.isEmpty(
                mWallpaperManager.wallpaperInfo.packageName
            )
        ) mWallpaperManager.wallpaperInfo.packageName else ""
    }

    fun saveVideoAndStartWallpaper(activity: Context, successInvoke:(Boolean)->Unit) {
        if (getDefaultWallpaperName(activity) == activity.packageName) {
            successInvoke.invoke(true)
            return
        }
        try {
            CoroutineScope(Dispatchers.IO).launch {
                var bitmap: Bitmap?
                try {
                    bitmap = getLauncherWallpaper(activity)
                } catch (var7: Exception) {
                    var7.printStackTrace()
                    bitmap = null

                }
                if (null != bitmap) {
                    val editPicDir = File(activity.filesDir, "wall")
                    editPicDir.mkdirs()
                    val wrapperFile = File(editPicDir.absolutePath + "/wall.jpeg")
                    wrapperFile.deleteOnExit()
                    try {
                        wrapperFile.createNewFile()
                        val outStream = FileOutputStream(wrapperFile)
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
                        outStream.flush()
                        outStream.close()
                        saveDefaultWallpaperPath(activity, wrapperFile.absolutePath)
                        withContext(Dispatchers.Main){
                            successInvoke.invoke(true)
                        }
                    } catch (var6: IOException) {
                        var6.printStackTrace()
                        withContext(Dispatchers.Main){

                            successInvoke.invoke(false)
                        }
                    }
                }else{
                    withContext(Dispatchers.Main){
                        successInvoke.invoke(false)
                    }
                }
            }
        } catch (var2: Exception) {
            var2.printStackTrace()
            successInvoke.invoke(false)
        }
    }

    private fun saveDefaultWallpaperPath(context: Context, str: String) {
        if (!str.isEmpty()) {
            val sp = context.getSharedPreferences("dwp", Context.MODE_PRIVATE)
            sp.edit().putString("d", str).apply()
        }
    }

    fun getDefaultWallpaperPath(context: Context): String? {
        val sp = context.getSharedPreferences("dwp", Context.MODE_PRIVATE)
        return sp.getString("d", "")
    }

    fun test() {
        val l = LottieDrawable()
        l.repeatCount
    }
}
